package com.assets.Restore;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.assets.Adapter.MyAdapter;
import com.assets.Ip_set.Url;
import com.assets.R;
import com.assets.util.TrustAllCerts;

import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/3/6.
 */

public class Restore extends Activity {
    private String url="http://"+ Url.IP+"/api/returns";
    private TextView state_one;
    private EditText property_one;
    private ListView lv_list;
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private Handler handler = new Handler();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.restore);
        inview();
        property_one.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (property_one.getText().toString().length()>5){
                    property_one.selectAll();
                }
                post();

            }
        });
    }
    public void post() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client =new OkHttpClient
                .Builder()
                .hostnameVerifier(new AllowAllHostnameVerifier())
                .sslSocketFactory(TrustAllCerts.createSSLSocketFactory(), TrustAllCerts.getX509TrustManager())
                .connectTimeout(10, TimeUnit.SECONDS)//连接超时
                .readTimeout(500, TimeUnit.SECONDS)
                .build();//创建OkHttpClient对象。
                FormBody.Builder formBody = new FormBody.Builder();//创建表单请求体
                formBody.add("assetNumber",property_one.getText().toString()); //传递键值对参数
                Request request = new Request.Builder()//创建Request 对象。
                        .url(url)
                        .post(formBody.build())//传递请求体
                        .build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        //回调方法的使用与get异步请求相同
                        Log.i("123123", String.valueOf(e));
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String responseData = response.body().string();
                        //回调方法的使用与get异步请求相同
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                        Log.i("123123", responseData);
                                    JSONObject json = new JSONObject(responseData);
                                    if (json.getString("state").equals("1"))
                                    {
                                        Map<String, Object> map = new HashMap<String, Object>();
                                        map.put("coding", json.getString("assetNumber"));
                                        map.put("coding_num", property_one.getText().toString());
                                        map.put("coding_sate", "成功");
                                        list.add(map);
                                        //适配器，刚刚重写的！
                                        Collections.reverse(list);
                                        MyAdapter myAdapter = new MyAdapter(Restore.this, list);
                                        //设置适配器
                                        lv_list.setAdapter(myAdapter);
                                    }
                                    if (json.getString("state").equals("0")){

                                        Map<String, Object> map = new HashMap<String, Object>();
                                        map.put("coding", json.getString("assetNumber"));
                                        map.put("coding_num", property_one.getText().toString());
                                        map.put("coding_sate", "失败");
                                        list.add(map);
                                        //适配器，刚刚重写的！
                                        Collections.reverse(list);
                                        MyAdapter myAdapter = new MyAdapter(Restore.this, list);
                                        //设置适配器
                                        lv_list.setAdapter(myAdapter);

                                    }
                                    if (json.getString("state").equals("2")){

                                        Map<String, Object> map = new HashMap<String, Object>();
                                        map.put("coding", json.getString("assetNumber"));
                                        map.put("coding_num", property_one.getText().toString());
                                        map.put("coding_sate", "不存在");
                                        list.add(map);
                                        //适配器，刚刚重写的！
                                        Collections.reverse(list);
                                        MyAdapter myAdapter = new MyAdapter(Restore.this, list);
                                        //设置适配器
                                        lv_list.setAdapter(myAdapter);

                                    }
                                    if (json.getString("state").equals("3")){

                                        Map<String, Object> map = new HashMap<String, Object>();
                                        map.put("coding", json.getString("assetNumber"));
                                        map.put("coding_num", property_one.getText().toString());
                                        map.put("coding_sate", "已归还");
                                        list.add(map);
                                        //适配器，刚刚重写的！
                                        Collections.reverse(list);
                                        MyAdapter myAdapter = new MyAdapter(Restore.this, list);
                                        //设置适配器
                                        lv_list.setAdapter(myAdapter);
                                    }
                                    if (json.getString("state").equals("4")){

                                        Map<String, Object> map = new HashMap<String, Object>();
                                        map.put("coding", json.getString("assetNumber"));
                                        map.put("coding_num", property_one.getText().toString());
                                        map.put("coding_sate", "不能归还");
                                        list.add(map);
                                        //适配器，刚刚重写的！
                                        Collections.reverse(list);
                                        MyAdapter myAdapter = new MyAdapter(Restore.this, list);
                                        //设置适配器
                                        lv_list.setAdapter(myAdapter);

                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }


                            }
                        });

                    }
                });

            }
        }).start();

    }

    private void inview() {
        lv_list = (ListView)findViewById(R.id.fragment2_list);
        state_one = (TextView)findViewById(R.id.state_one);
        property_one = (EditText)findViewById(R.id.property_one);
    }
}
